package com.chinemit.cui.components.button
{
	
	import com.chinemit.cui.components.core.CUIComponent;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 *  
	 * @author chenying
	 * 
	 */
	public class ImageButton extends CUIComponent
	{
		
		private var imgContainer:Bitmap;
		private var overImg:Bitmap;
		private var downImg:Bitmap;
		private var curImg:Bitmap;
		
		public function ImageButton(_img:Bitmap,_overImg:Bitmap,_downImg:Bitmap,_buttonMode:Boolean = true)
		{
			buttonMode = _buttonMode;
			imgContainer = _img;
			overImg = _overImg;
			downImg = _downImg;
			
		}
		
		override protected function into():void{
			super.into();
			updateImg(imgContainer);
			this.addEventListener(MouseEvent.MOUSE_OVER,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
			this.addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
		}
		
		override protected function out():void{
			super.out();
			this.addEventListener(Event.ADDED_TO_STAGE,eventHandler); 
//			dispose();
		}
			
		/**
		 * 更新图像
		 * @param img
		 * 
		 */		
		private function updateImg(img:Bitmap):void{
			if(curImg!=null){
				if(contains(curImg)){
					removeChild(curImg);	
				}
			}
			addChild(img);
			curImg = img;
		}
		
		/**
		 * 
		 * @param e
		 *  
		 */		
		private function mouseEventHandler(e:MouseEvent):void{
			switch(e.type){
				case MouseEvent.MOUSE_OVER:
					updateImg(overImg);
					break;
				case MouseEvent.MOUSE_OUT:
					updateImg(imgContainer);
					break;
				case MouseEvent.MOUSE_UP:
					updateImg(imgContainer);
					break;
				case MouseEvent.MOUSE_DOWN:
					updateImg(downImg);
					break;
			}
		}
		
		/**
		 * 销毁
		 * 
		 */		
		private function dispose():void{
			overImg.bitmapData.dispose();
			downImg.bitmapData.dispose();
			imgContainer.bitmapData.dispose();
			overImg = null;
			downImg = null;
			imgContainer = null;
		}
	}
}